﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.AdminCP;
using SES.CMS.BL;
using SES.CMS.DAL;
using SES.CMS.DO;   

namespace SES.CMS.AdminCP
{
    public partial class ListUser : System.Web.UI.Page
    {
        sysUserDO objUser = new sysUserDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                View();
            }
        }

        void View()
        {
            gridView.DataSource = new sysUserBL().SelectAll();
            gridView.DataBind();
            details.Visible = false;
            btnThemMoi.Visible = true;
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
        }


        protected string ReturnType(object e)
        {
            string sReturn = "";
            try
            {
                int type = int.Parse(e.ToString());
                if (type == 0)
                    sReturn = "Admin";
                else
                    sReturn = "Nhân viên";
                
            }
            catch
            {
            }
            return sReturn;
        }
        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            objUser = new sysUserBL().Select(objUser);
            int UserId = Convert.ToInt32(e.CommandArgument);
            Session["UserId"] = UserId;
            objUser.UserID = UserId;
            switch (e.CommandName)
            {
                case "Select":
                    Response.Redirect("User.aspx?UserID="+ UserId.ToString());
                    break;
                case "Delete":
                    new sysUserBL().Delete(objUser);
                    Functions.Alert("Xóa thành công!!!");
                    View();
                    break;
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gridView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            Response.Redirect("User.aspx");
            //details.Visible = true;
            //ltAddress.Text = "";
            //ltEmail.Text = "";
            //ltFullname.Text = "";
            //ltPassrepeat.Text = "";
            //ltPassword.Text = "";
            //ltUsername.Text = "";
            //ltUsername.Focus();
            //ltYahoo.Text = "";
            //btnThemMoi.Visible = false;
        }

        protected void btnCapnhat_Click(object sender, EventArgs e)
        {
            objUser = new sysUserBL().Select(objUser);
            objUser.Username = ltUsername.Text;
            objUser.UserType = 1;
            objUser.YahooIM = ltYahoo.Text;
            objUser.FullName = ltFullname.Text;
            objUser.Password = Functions.EncryptMd5(ltPassword.Text); 
            objUser.Email = ltEmail.Text;
            objUser.Address = ltAddress.Text;
            if (rdCheck.Checked)
                objUser.IsActive = true;
            else if (rdCheck1.Checked)
                objUser.IsActive = false;
            new sysUserBL().Insert(objUser);
            Functions.Alert("Thêm mới thành công!!!");
            View();
        }

        protected void btnSua_Click(object sender, EventArgs e)
        {
            View();
        }
    }
}